#!/bin/bash

# Test that distributed make works in a SLURM allocation using mpirun.

# Copyright (c) Los Alamos National Security, LLC, and others.

. ./environment.sh

cd $DATADIR
exec 2>&1


## Preliminary logistics

# sshrot should claim the correct mechanism
x sshrot --info

# SSH multiplexing should not work
x sshrot --muxstart || true
x sshrot --muxstop || true


## Does sshrot work on a basic level? ##

# Run three tasks.
y "sshrot 'sleep 1 && echo a && sleep 1 && echo b' &"
y "sshrot 'sleep 1 && echo a && sleep 1 && echo b' &"
y "sshrot 'sleep 1 && echo a && sleep 1 && echo b' &"
# Wait for "Reported: ..."
x sleep 0.5
# Wait for "a".
x sleep 1.0
# Wait for "b".
x sleep 1.0

# Run two tasks; they should run on different hosts.
y "sshrot 'hostname > hostname.1'"
y "sshrot 'hostname > hostname.2'"
y "cmp --quiet hostname.1 hostname.2 || echo commands ran on different nodes"


## QUACreduce test ##

echo -e 'foo bar baz\nfoo foo' > foo1.txt
echo -e 'bar' > foo2.txt
quacreduce --dist \
           --map 'tr "[:blank:]" "\n"' \
           --reduce 'uniq -c > out/%(RID)' \
           --partitions 3 \
           foo*.txt
x make -j2 --quiet
y 'cat out/* | sort'
